package com.zm.Study02_FileInputStreamTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamDemo02 {
    FileInputStream fis=new FileInputStream("D:\\project\\IDEAPROJECT\\JavaIO\\src\\main\\resources\\static\\FileInputStreamTxt\\fis.txt");

    public FileInputStreamDemo02() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        FileInputStreamDemo02 fileInputStreamDome02=new FileInputStreamDemo02();
      //  fileInputStreamDome02.readBytes();
        fileInputStreamDome02.readBytesWhile();

    }

    //读取数组
    public void readBytes() throws IOException {
        byte[] bytes=new byte[5];
        //文件内容为 hello world
        //第一次读取
        int len= fis.read(bytes); //“读取到”的字符的个数  换行和空格会被读取出来（作为一个字符，末尾的空格会清除，换行不会\r\n  or \r or  \n）
        System.out.println(len);
        System.out.println(new String(bytes,0,len));
        //第二次读取
        len= fis.read(bytes);
        System.out.println(len);
        System.out.println(new String(bytes,0,len));
        //第三次读取
        len= fis.read(bytes);
        System.out.println(len);
        System.out.println(new String(bytes,0,len));

        //第四次读取 (第三次就已经读取完毕了，所以这次值为-1)
        len= fis.read(bytes);
        System.out.println(len);

        fis.close();
        //直接new string读取出来的东西会混乱
    }

    public void readBytesWhile() throws IOException {
        byte[] bytes=new byte[1024];
        int len;
        while ((len=fis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        fis.close();
    }
}
